package com.edou.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.base.util.CommonUtil;
import com.edou.member.dao.IMemberCommentDao;
import com.edou.member.entity.MemberComment;
import com.edou.member.service.IMemberCommentService;

import com.edou.member.dto.MemberCommentCondition;

/**
 * 会员评论
 * @creator     
 * @create-time 2016/4/18 16:26:28
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("memberCommentServiceImpl")
@Transactional(value = "transactionManager")
public class MemberCommentServiceImpl implements IMemberCommentService{

	@Resource(name = "memberCommentDaoImpl")
	private IMemberCommentDao memberCommentDao;

	/**
	 * 查询所有会员评论
	 */
	public List<MemberComment> listMemberCommentAll(){
		return memberCommentDao.listMemberCommentAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listMemberCommentByPage(PageResult<MemberComment> pageResult,MemberCommentCondition condition){
		int rows = memberCommentDao.listMemberCommentByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberComment> list = memberCommentDao.listMemberCommentByPage(rowBounds,condition);
		
		// 格式化发布时间，比如：发布于XXX分钟前
		for(MemberComment comment : list){
			String postTimeStr = CommonUtil.friendlyTime(comment.getCreateDate());
			comment.setPostTimeStr(postTimeStr);
		}
		
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 会员评论
	 */
	public MemberComment getMemberCommentById(Long id){
		return memberCommentDao.getMemberCommentById(id);
	}

	/**
	 * 新增
	 * @param item 会员评论
	 */
	public void add(MemberComment item){
		memberCommentDao.add(item);
	}

	/**
	 * 修改
	 * @param item 会员评论
	 */
	public void update(MemberComment item){
		memberCommentDao.update(item);
	}
	
	/**
	 * 修改评论公布状态：0-非公开，1-公开
	 * @param id 会员评论ID
	 * @param state 公布状态：0-非公开，1-公开
	 */
	public void updateState(Long id, Integer state) {
		memberCommentDao.updateState(id, state);
	}

	/**
	 * 修改点赞数
	 * @param id 		会员评论ID
	 * @param countLike 点赞数：1-点赞，(-1)-取消点赞
	 */
	public void updateCountLike(Long id, Integer countLike) {
		memberCommentDao.updateCountLike(id, countLike);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		memberCommentDao.delByIds(ids);
	}

	/**
	 * 根据评论对象ID和评论类型查询
	 * @param objectId
	 * @param objectType
	 * @return
	 */
	public MemberComment getMemberCommentByObjectIdAndObjectType(String objectId, Integer objectType) {
		return memberCommentDao.getMemberCommentByObjectIdAndObjectType(objectId, objectType);
	}

	/**
	 * 查询文章的评论数
	 */
	@Override
	public int getMemberCommentByObjectIdCount(String objectId) {
		return memberCommentDao.getMemberCommentByObjectIdCount(objectId);
	}

	/**
	 * 根据评论对象ID分组分页查询数据
	 */
	@Override
	public void listMemberCommentGroupObjectIdByPage(PageResult<MemberComment> pageResult,
			MemberCommentCondition condition) {
		int rows = memberCommentDao.listMemberCommentGroupObjectIdByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberComment> list = memberCommentDao.listMemberCommentGroupObjectIdByPage(rowBounds,condition);
		pageResult.setResult(list);
		
	}

	/**
	 * 根据条件查询
	 */
	@Override
	public List<MemberComment> listMemberCommentByCondition(MemberCommentCondition condition) {
		return memberCommentDao.listMemberCommentByCondition(condition);
	}

	/**
	 * 根据会员ID和评论对象ID进行删除
	 */
	@Override
	public void delByMemberIdAndObjectId(String memberUserId, String objectId) {
		memberCommentDao.delByMemberIdAndObjectId(memberUserId, objectId);
	}

}

